
<template>
<div>
  <van-nav-bar
    title="收货地址"
    left-arrow
    @click-left="return1"
    :fixed="true" :placeholder="true"
  />
  <van-address-edit
    :area-list="areaList"
    :address-info="address"
    show-delete
    show-set-default
    show-search-result
    :search-result="searchResult"
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
    @delete="onDelete"
  />
</div>
</template>
<script>
import Vue from 'vue';
import { AddressEdit } from 'vant';
import { NavBar } from 'vant';
Vue.use(NavBar);
Vue.use(AddressEdit);
import { Toast } from 'vant';
import {areaList} from "@vant/area-data";
import request from "../../utils/request";

export default {
  data() {
    return {
      orderId:this.$route.query.orderId,
      i:this.$route.query.i,//detailId
      itemId:this.$route.query.itemId,
      address:{
        name:this.$route.query.name,
        id:this.$route.query.id,
        isDefault:this.$route.query.isDefault,
        addressDetail:this.$route.query.addressDetail,
        county:this.$route.query.county,
        city:this.$route.query.city,
        province:this.$route.query.province,
        tel:this.$route.query.tel,
        areaCode:this.$route.query.areaCode
      },
      areaList,
      searchResult: [],
    };
  },
  methods: {
    onSave(addressInfo) {
      addressInfo.recipientName=addressInfo.name
      addressInfo.phoneNumber=addressInfo.tel
      addressInfo.district=addressInfo.isDefault?"1":"0"
      addressInfo.streetAddress=addressInfo.addressDetail
      addressInfo.addressId=addressInfo.id
      request.post('/address/addRess',addressInfo).then(resp=>{
        if (resp.code===0){
          if (this.itemId){
            this.$router.push({path:'/address',query:{i:this.i,itemId:this.itemId,orderId:this.orderId}})
          }else{
            this.$router.push({path:'/address',query:{orderId:this.orderId}})
          }
          Toast(resp.errorMessage);
        }
      })
    },
    return1(){
      if (this.itemId){
        this.$router.push({path:'/address',query:{i:this.i,itemId:this.itemId,orderId:this.orderId}})
      }else {
        this.$router.push({path:'/address',query:{orderId:this.orderId}})
      }

    },
    onDelete() {
      Toast('delete');
    },
  },
};
</script>
<style scoped>
>>> .van-nav-bar .van-icon{
  color: black!important;
}
</style>
